<?php
namespace app\admin\controller;
use think\Db;
use app\admin\model\DepartmentModel;
class Icdata extends Base
{
	/**
	 * [公共]
	 */
	# -< Common >---------------------------------------------------------------
	public function getCurDepart() {
		return DB::name('department')->alias('depart')
			->join('role role','depart.id = role.department_id')
			->join('user user','user.role_id = role.id')
			->where('user.id',session('id'))
			->field('depart.name,depart.id')->find();
	}

	public function getDepartOpt() {
		$DepartModel = new DepartmentModel();
		$departs = $DepartModel->getCanSeeDepart(session('id'));
		$departs = $this->arr2tree($departs);
		$depart_opt = $this->getNameByTree($departs);
		return $depart_opt;
	}

	/**
	 * [行业列表]
	 */
	# -< 县域零售业 >------------------------------------------------------------
	public function cate() {
		if($this->request->isPost()){
			$param = input('post.');
			$limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
			$data['rows'] = DB::name('icdata_cate')->limit($offset,$limit)->order('cate asc')->select();
			foreach($data['rows'] as &$v){
				switch ($v['cate']) {
					case '1':
						$v['cate_name'] = "实业型";break;
					case '2':
						$v['cate_name'] = "服务型";break;
					default:
						$v['cate_name'] = "不详";break;
						break;
				}
			}
			$data['total'] = DB::name('icdata_cate')->count();
			return ['code'=>1,'msg'=>'加载成功','data'=>$data];
		}
		$departs = $this->getDepartOpt();
		$this->assign([
			'departs' => $departs
		]);
		return $this->fetch();
	}

	public function cateAdd() {
		$param = input("post.");
		$res = DB::name('icdata_cate')->insert($param);
		if($res !== false){
			return ['code'=>1,'msg'=>'添加成功'];
		}else{
			return ['code'=>0,'msg'=>'添加失败'];
		}
	}

	public function cateEdit() {
		$param = input("post.");
		$res = DB::name('icdata_cate')->where('id',$param['id'])->update($param);
		if($res !== false){
			return ['code'=>1,'msg'=>'编辑成功'];
		}else{
			return ['code'=>0,'msg'=>'编辑失败'];
		}
	}

	public function cateDel() {
		$param = input("post.");
		$res = DB::name('icdata_cate')->where('id',$param['id'])->delete();
		if($res !== false){
			return ['code'=>1,'msg'=>'删除成功'];
		}else{
			return ['code'=>0,'msg'=>'删除失败'];
		}
	}

	/**
	 * [县域电商零售业]
	 */
	public function commit() {
		return $this->fetch();
	}

	public function isSave() {
		$param = input("post.");
		$depart = $this->getCurDepart();
		$where = [
			'depart_id' => $depart['id'],
			'rpt_date' => strtotime($param['rpt_date'] . "-01 00:00:00")
		];
		$count = DB::name('icdata_table')->where($where)->count();
		if($count != 0){
			return ['code'=>0,'msg'=>'该月份的报表已填写'];
		}else{
			return ['code'=>1,'msg'=>'ok'];
		}
	}

	public function writeTable() {
		$this->assign([
			'depart' => $this->getCurDepart(),
			'rpt_date' => input('get.rpt_date'),
		]);
		return $this->fetch();
	}

	public function getCateOpt() {
		$param = input('post.');
		$data = DB::name("icdata_cate")->where('cate',$param['cate'])->select();
		return ['code'=>1,'data'=>$data];
	}

	public function saveTable() {
		$param = input('post.');
		if($param['link_man']==''){
			return ['code'=>0,'msg'=>'联系人不能为空'];
		}
		if($param['link_type']==''){
			return ['code'=>0,'msg'=>'联系方式不能为空'];
		}
		if($param['rpt_date']==''){
			return ['code'=>0,'msg'=>'报表日期不能为空'];
		}
		foreach($param['tableCT'] as &$v){
			unset($v['opt']);
		}
		$content = $this->Arr2XML($param['tableCT']);
		$data = $param;
		unset($data['tableCT']);
		$data['content'] = $content;
		$data['rpt_date'] = strtotime($data['rpt_date']."-01 00:00:00");
		DB::startTrans();
		try {
			$where = [
				'depart_id' => $data['depart_id'],
				'rpt_date' => $data['rpt_date']
			];
			$count = DB::name('icdata_table')->where($where)->count();
			if($count == 0){
				DB::name("icdata_table")->insert($data);
			}else{
				DB::name("icdata_table")->where($where)->update($data);
			}
			DB::commit();
			return ['code'=>1,'msg'=>'保存成功'];
		} catch (\Exception $e) {
			DB::rollback();
			return ['code'=>0,'msg'=>'保存失败:'.$e->getMessage()];
		}
	}

	public function editTable() {
		if($this->request->isPost()){
			$table_id = input("post.table_id");
			$table = DB::name('icdata_table')->where('id',$table_id)->find();
			$table['tableCT'] = $this->XML2Arr($table['content']);
			foreach($table['tableCT'] as &$v){
				$v['opt'] = '';
			}
			unset($table['content']);
			$table['rpt_date'] = date('Y-m',$table['rpt_date']);
			return ['code'=>1,'msg'=>'ok','data'=>$table];
		}
		$this->assign([
			'table_id' => input('id')
		]);
		return $this->fetch();
	}

	public function delTable() {
		$param = input("post.");
		$res = DB::name("icdata_table")->where('id',$param['id'])->delete();
		if($res !== false){
			return ['code'=>1,'msg'=>'删除成功'];
		}else{
			return ['code'=>0,'msg'=>'删除失败'];
		}
	}

	public function list() {
		if($this->request->isPost()){
			$param = input("post.");
			$limit = $param['pageSize'];
			$offset = ($param['pageNumber'] - 1) * $limit;
			$where = [];
			if($param['seaDepart'] != ''){
				$where['depart_id'] = $param['seaDepart'];
			}
			if($param['seaTime'][0] != '' && $param['seaTime'][1] != ''){
				$param['seaTime'][0] = $param['seaTime'][0] / 1000;
				$param['seaTime'][1] = $param['seaTime'][1] / 1000;
				$where['rpt_date'] = ['between',$param['seaTime']];
			}
			if($param['seaLinkman'] != ""){
				$where['link_man'] = ['like',"%".$param['seaLinkman']."%"];
			}
			$data['rows'] = DB::name('icdata_table')
				->where($where)->limit($offset,$limit)->select();
			foreach($data['rows'] as &$v){
				$v['rpt_date'] = date("Y-m",$v['rpt_date']);
			}
			$data['total'] = DB::name('icdata_table')->where($where)->count();
			return ['code'=>1,'msg'=>'加载成功','data'=>$data];
		}
		return $this->fetch();
	}

	/**
	 * [云南电商整体交易]
	 */
	# -< 云南电商整体交易 >------------------------------------------------------
	public function commit_all() {
		if($this->request->isAjax()){
			$param = input("post.");
			$depart = $this->getCurDepart();
			$where = [
				'depart_id' => $depart['id'],
				'rpt_date' => strtotime($param['rpt_date'] . "-01 00:00:00")
			];
			$count = DB::name('icdata_table_all')->where($where)->count();
			if($count != 0){
				return ['code'=>0,'msg'=>'该月份的报表已填写'];
			}else{
				return ['code'=>1,'msg'=>'ok'];
			}
		}
		return $this->fetch();
	}

	public function writeTable_all() {
		$this->assign([
			'depart' => $this->getCurDepart(),
			'rpt_date' => input('get.rpt_date'),
		]);
		return $this->fetch();
	}

	public function saveTable_all() {
		$param = input('post.');
		if($param['link_man']==''){
			return ['code'=>0,'msg'=>'联系人不能为空'];
		}
		if($param['link_type']==''){
			return ['code'=>0,'msg'=>'联系方式不能为空'];
		}
		if($param['rpt_date']==''){
			return ['code'=>0,'msg'=>'报表日期不能为空'];
		}
		$content = $this->Arr2XML($param['tableCT']);
		$data = $param;
		unset($data['tableCT']);
		$data['content'] = $content;
		$data['rpt_date'] = strtotime($data['rpt_date']."-01 00:00:00");
		DB::startTrans();
		try {
			$where = [
				'depart_id' => $data['depart_id'],
				'rpt_date' => $data['rpt_date']
			];
			$count = DB::name('icdata_table_all')->where($where)->count();
			if($count == 0){
				DB::name("icdata_table_all")->insert($data);
			}else{
				DB::name("icdata_table_all")->where($where)->update($data);
			}
			DB::commit();
			return ['code'=>1,'msg'=>'保存成功'];
		} catch (\Exception $e) {
			DB::rollback();
			return ['code'=>0,'msg'=>'保存失败:'.$e->getMessage()];
		}
	}

	public function list_all() {
		if($this->request->isPost()){
			$param = input("post.");
			$limit = $param['pageSize'];
			$offset = ($param['pageNumber'] - 1) * $limit;
			$where = [];
			if($param['seaDepart'] != ''){
				$where['depart_id'] = $param['seaDepart'];
			}
			if($param['seaTime'][0] != '' && $param['seaTime'][1] != ''){
				$param['seaTime'][0] = $param['seaTime'][0] / 1000;
				$param['seaTime'][1] = $param['seaTime'][1] / 1000;
				$where['rpt_date'] = ['between',$param['seaTime']];
			}
			if($param['seaLinkman'] != ""){
				$where['link_man'] = ['like',"%".$param['seaLinkman']."%"];
			}
			$data['rows'] = DB::name('icdata_table_all')
				->where($where)->limit($offset,$limit)->select();
			foreach($data['rows'] as &$v){
				$v['rpt_date'] = date("Y-m",$v['rpt_date']);
			}
			$data['total'] = DB::name('icdata_table_all')->where($where)->count();
			return ['code'=>1,'msg'=>'加载成功','data'=>$data];
		}
		return $this->fetch();
	}

	public function editTable_all() {
		if($this->request->isPost()){
			$table_id = input("post.table_id");
			$table = DB::name('icdata_table_all')->where('id',$table_id)->find();
			$table['tableCT'] = $this->XML2Arr($table['content']);
			unset($table['content']);
			$table['rpt_date'] = date('Y-m',$table['rpt_date']);
			return ['code'=>1,'msg'=>'ok','data'=>$table];
		}
		$this->assign([
			'table_id' => input('id')
		]);
		return $this->fetch();
	}

	public function delTable_all() {
		$param = input("post.");
		$res = DB::name("icdata_table_all")->where('id',$param['id'])->delete();
		if($res !== false){
			return ['code'=>1,'msg'=>'删除成功'];
		}else{
			return ['code'=>0,'msg'=>'删除失败'];
		}
	}

	/**
	 * [县域电商整体交易]
	 */
	# -< 县域电商整体交易 >------------------------------------------------------
	public function commit_all_county() {
		if($this->request->isAjax()){
			$param = input("post.");
			$depart = $this->getCurDepart();
			$where = [
				'depart_id' => $depart['id'],
				'rpt_date' => strtotime($param['rpt_date'] . "-01 00:00:00")
			];
			$count = DB::name('icdata_table_all_county')->where($where)->count();
			if($count != 0){
				return ['code'=>0,'msg'=>'该月份的报表已填写'];
			}else{
				return ['code'=>1,'msg'=>'ok'];
			}
		}
		return $this->fetch();
	}

	public function writeTable_all_county() {
		$this->assign([
			'depart' => $this->getCurDepart(),
			'rpt_date' => input('get.rpt_date'),
		]);
		return $this->fetch();
	}

	public function saveTable_all_county() {
		$param = input('post.');
		if($param['link_man']==''){
			return ['code'=>0,'msg'=>'联系人不能为空'];
		}
		if($param['link_type']==''){
			return ['code'=>0,'msg'=>'联系方式不能为空'];
		}
		if($param['rpt_date']==''){
			return ['code'=>0,'msg'=>'报表日期不能为空'];
		}
		$content = $this->Arr2XML($param['tableCT']);
		$data = $param;
		unset($data['tableCT']);
		$data['content'] = $content;
		$data['rpt_date'] = strtotime($data['rpt_date']."-01 00:00:00");
		DB::startTrans();
		try {
			$where = [
				'depart_id' => $data['depart_id'],
				'rpt_date' => $data['rpt_date']
			];
			$count = DB::name('icdata_table_all_county')->where($where)->count();
			if($count == 0){
				DB::name("icdata_table_all_county")->insert($data);
			}else{
				DB::name("icdata_table_all_county")->where($where)->update($data);
			}
			DB::commit();
			return ['code'=>1,'msg'=>'保存成功'];
		} catch (\Exception $e) {
			DB::rollback();
			return ['code'=>0,'msg'=>'保存失败:'.$e->getMessage()];
		}
	}

	public function list_all_county() {
		if($this->request->isPost()){
			$param = input("post.");
			$limit = $param['pageSize'];
			$offset = ($param['pageNumber'] - 1) * $limit;
			$where = [];
			if($param['seaDepart'] != ''){
				$where['depart_id'] = $param['seaDepart'];
			}
			if($param['seaTime'][0] != '' && $param['seaTime'][1] != ''){
				$param['seaTime'][0] = $param['seaTime'][0] / 1000;
				$param['seaTime'][1] = $param['seaTime'][1] / 1000;
				$where['rpt_date'] = ['between',$param['seaTime']];
			}
			if($param['seaLinkman'] != ""){
				$where['link_man'] = ['like',"%".$param['seaLinkman']."%"];
			}
			$data['rows'] = DB::name('icdata_table_all_county')
				->where($where)->limit($offset,$limit)->select();
			foreach($data['rows'] as &$v){
				$v['rpt_date'] = date("Y-m",$v['rpt_date']);
			}
			$data['total'] = DB::name('icdata_table_all_county')->where($where)->count();
			return ['code'=>1,'msg'=>'加载成功','data'=>$data];
		}
		return $this->fetch();
	}

	public function editTable_all_county() {
		if($this->request->isPost()){
			$table_id = input("post.table_id");
			$table = DB::name('icdata_table_all_county')->where('id',$table_id)->find();
			$table['tableCT'] = $this->XML2Arr($table['content']);
			unset($table['content']);
			$table['rpt_date'] = date('Y-m',$table['rpt_date']);
			return ['code'=>1,'msg'=>'ok','data'=>$table];
		}
		$this->assign([
			'table_id' => input('id')
		]);
		return $this->fetch();
	}

	public function delTable_all_county() {
		$param = input("post.");
		$res = DB::name("icdata_table_all_county")->where('id',$param['id'])->delete();
		if($res !== false){
			return ['code'=>1,'msg'=>'删除成功'];
		}else{
			return ['code'=>0,'msg'=>'删除失败'];
		}
	}

	/**
	 * [县域电商解析]
	 */
	# -< 县域电商解析 >------------------------------------------------------
	public function commit_analyze_county() {
		if($this->request->isAjax()){
			$param = input("post.");
			$depart = $this->getCurDepart();
			$where = [
				'depart_id' => $depart['id'],
				'rpt_date' => strtotime($param['rpt_date'] . "-01 00:00:00")
			];
			$count = DB::name('icdata_table_analyze_county')->where($where)->count();
			if($count != 0){
				return ['code'=>0,'msg'=>'该月份的报表已填写'];
			}else{
				return ['code'=>1,'msg'=>'ok'];
			}
		}
		return $this->fetch();
	}

	public function writeTable_analyze_county() {
		if($this->request->isAjax()){
			$data = DB::name("users_government")->where("level",3)
				->field("depart_id,depart_name")->select();
			foreach($data as &$v){
				$v = [
					'depart_id' => $v['depart_id'],
					'depart_name' => $v['depart_name'],
					'net_money' => "",
					'industry_money' => "",
					'service_money' => "",
					'net_rank' => "",
					'industry_rank' => "",
					'service_rank' => "",
				];
				return ['code'=>1,'msg'=>'ok','data'=>$data];
			}
		}
		$this->assign([
			'depart' => $this->getCurDepart(),
			'rpt_date' => input('get.rpt_date'),
		]);
		return $this->fetch();
	}

	public function saveTable_analyze_county() {
		$param = input('post.');
		if($param['link_man']==''){
			return ['code'=>0,'msg'=>'联系人不能为空'];
		}
		if($param['link_type']==''){
			return ['code'=>0,'msg'=>'联系方式不能为空'];
		}
		if($param['rpt_date']==''){
			return ['code'=>0,'msg'=>'报表日期不能为空'];
		}
		$content = $this->Arr2XML($param['tableCT']);
		$data = $param;
		unset($data['tableCT']);
		$data['content'] = $content;
		$data['rpt_date'] = strtotime($data['rpt_date']."-01 00:00:00");
		DB::startTrans();
		try {
			$where = [
				'depart_id' => $data['depart_id'],
				'rpt_date' => $data['rpt_date']
			];
			$count = DB::name('icdata_table_analyze_county')->where($where)->count();
			if($count == 0){
				DB::name("icdata_table_analyze_county")->insert($data);
			}else{
				DB::name("icdata_table_analyze_county")->where($where)->update($data);
			}
			DB::commit();
			return ['code'=>1,'msg'=>'保存成功'];
		} catch (\Exception $e) {
			DB::rollback();
			return ['code'=>0,'msg'=>'保存失败:'.$e->getMessage()];
		}
	}

	public function list_analyze_county() {
		if($this->request->isPost()){
			$param = input("post.");
			$limit = $param['pageSize'];
			$offset = ($param['pageNumber'] - 1) * $limit;
			$where = [];
			if($param['seaDepart'] != ''){
				$where['depart_id'] = $param['seaDepart'];
			}
			if($param['seaTime'][0] != '' && $param['seaTime'][1] != ''){
				$param['seaTime'][0] = $param['seaTime'][0] / 1000;
				$param['seaTime'][1] = $param['seaTime'][1] / 1000;
				$where['rpt_date'] = ['between',$param['seaTime']];
			}
			if($param['seaLinkman'] != ""){
				$where['link_man'] = ['like',"%".$param['seaLinkman']."%"];
			}
			$data['rows'] = DB::name('icdata_table_analyze_county')
				->where($where)->limit($offset,$limit)->select();
			foreach($data['rows'] as &$v){
				$v['rpt_date'] = date("Y-m",$v['rpt_date']);
			}
			$data['total'] = DB::name('icdata_table_analyze_county')->where($where)->count();
			return ['code'=>1,'msg'=>'加载成功','data'=>$data];
		}
		return $this->fetch();
	}

	public function editTable_analyze_county() {
		if($this->request->isPost()){
			$table_id = input("post.table_id");
			$table = DB::name('icdata_table_analyze_county')->where('id',$table_id)->find();
			$table['tableCT'] = $this->XML2Arr($table['content']);
			unset($table['content']);
			$table['rpt_date'] = date('Y-m',$table['rpt_date']);
			return ['code'=>1,'msg'=>'ok','data'=>$table];
		}
		$this->assign([
			'table_id' => input('id')
		]);
		return $this->fetch();
	}

	public function delTable_analyze_county() {
		$param = input("post.");
		$res = DB::name("icdata_table_analyze_county")->where('id',$param['id'])->delete();
		if($res !== false){
			return ['code'=>1,'msg'=>'删除成功'];
		}else{
			return ['code'=>0,'msg'=>'删除失败'];
		}
	}
}
